Using Xbasic to Control Access to a Form

Description

The Invoice form contains the btnEditItems0 ( Zoom ... ) button. The Xbasic code run by the button's OnPush event creates and sets the value of the global variable varInvoice_Number, then displays the Items form (from the AlphaSports sample database). The result is that the global variable varInvoice_Number only exists when the Items form is called by the Invoice form.

Xbasic Code Run by the btnEnter OnPush Event

if (eval_valid("varInvoice_Number") = .t.) then
    topparent:invoice_number.enable()
    topparent.New_Record()
    topparent:Invoice_Number.text = varInvoice_Number
    topparent:Invoice_Number.disable()
    topparent:Product_Id.activate()
    btnEnter.hide()
    btnSave.show()
else
    msg = "You can only use this form to enter a new line item if you open it from a button on the Invoice form."
    ui_msg_box("Error",msg,UI_STOP_SYMBOL)
end if

Xbasic Code Run by the btnEnter OnPush Event

The first line of code uses the eval_valid() function to see if the varInvoice_Number variable can be evaluated, which is the same as saying that the variable exists. The variable only exists if the Invoice form created it before opening the Items form.

if (eval_valid("varInvoice_Number") = .t.) then

If the above expression evaluates to true, enable the invoice_number field so it can receive focus and be edited.

topparent:invoice_number.enable()

Create a new record.

topparent.New_Record()

Set the value of the invoice_number field to varInvoice_Number.

topparent:Invoice_Number.text = varInvoice_Number

Disable the invoice_number field so it cannot be edited.

topparent:Invoice_Number.disable()

Set focus to the product_id field so it can accept data.

topparent:Product_Id.activate()

Hide this button, which is named btnEnter (Enter New Item).

btnEnter.hide()

Show the other button in the stack.

btnSave.show()

If we couldn't evaluate the varInvoice_Number variable, display an error message.

else
    msg = "You can only use this form to enter a new line item if you open it from a button on the Invoice form."
    ui_msg_box("Error",msg,UI_STOP_SYMBOL)
end if

Xbasic Code Run by the btnSave OnPush Event

The btnSave OnPush event is the same as the btnEnter OnPush event, except the it hides btnSave and shows bntEnter.

.
' Show btnEnter (Enter New Item)
btnEnter.show()
' Hide btnSave (Save Record)
btnSave.hide()
.
.

See Also